<?php
/**
 * 发送Email
 *
 */

require_once(dirname(__FILE__) . '/../newwww/config.php'); 
require_once(FITWOW_ROOT_PATH . 'apps/common/Mail.class.php');   

function get_mysqli() {
    global $cfg_db;
    $ret = new mysqli($cfg_db['hosts']['host'], $cfg_db['hosts']['username'],
        $cfg_db['hosts']['password'], $cfg_db['hosts']['dbname'], $cfg_db['hosts']['port']);
    if (mysqli_connect_errno()) {
        echo "can't connect mysql\n";
        exit;
    }
    return $ret;
}

function send_email()
{
    $mysql_handler = get_mysqli();
    $strSql = 'SELECT * FROM tcs_emails WHERE status=0';
    $strUpdateSql = 'UPDATE tcs_emails set status =1 WHERE id=%d';
    $result = $mysql_handler->query($strSql);
    if ($result === false) {
        trigger_error("send_email error. [sql:".$strSql."]");
    }
    
    
    $objEmailSender = new Mail();
    while ($email = $result->fetch_assoc()) {
        $ret = $objEmailSender->sendSmtpMail($email['to_addr'], $email['subject'], $email['content']);
        $strLog = sprintf("send_email.[id:%d] [to_addr:%s] [subject:%s] [ret:%d] [time:%s]",
                $email['id'], $email['to_addr'], $email['subject'], $ret, date("Y-m-d H:i:s"));
        $strUpdateSql = sprintf($strUpdateSql, intval($email['id']));
        $mysql_handler->query($strUpdateSql);
        trigger_error($strLog);
    }
    $result->free();
    $mysql_handler->close();
}
//start
send_email();

?>
